package com.baike.controller;

import com.baike.data.Result;
import com.baike.em.ConstantEnum;
import com.baike.entity.Menu;
import com.baike.service.IMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author x_FG
 * @since 2021-10-28
 */
@RestController
@RequestMapping("/menu")
@Api(value = "权限菜单接口")
@RefreshScope
public class MenuController {

    @Autowired
    private IMenuService iMenuService;


    @ApiOperation("权限列表查询方法")
    @GetMapping("treeView")
    public Result getMenuInfo(){
        List<Map<String, Object>> list = iMenuService.getMenuInfo();
        return Result.build(ConstantEnum.SUCCESS).setResult(list);
    }

    @ApiOperation("权限菜单查询方法")
    @GetMapping("menu")
    public Result getMenuZtree(){
        List<Map<String,Object>> list = iMenuService.getMenuZtree();
        return Result.build(ConstantEnum.SUCCESS).setResult(list);
    }

    @ApiOperation("权限菜单增加方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "menu主键Id",required = true,paramType = "path"),
            @ApiImplicitParam(name = "menu",value = "menu实体类",required = true,paramType = "body")
    })
    @PostMapping("/{id}")
    public Result insertMenuInfo(@PathVariable("id")Integer id,@RequestBody Menu menu){
        menu.setPid(id);
        boolean b = iMenuService.insertMenuInfo(menu);
        if (b){
            return Result.build(ConstantEnum.SUCCESS);
        }
        return Result.build(ConstantEnum.ERROR);
    }

    @ApiOperation("权限菜单根据Id查询方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "menu主键Id",required = true,paramType = "path")
    })
    @GetMapping("ID/{id}")
    public Result getMenuById(@PathVariable("id")Integer id){
        Menu menu = iMenuService.getMenuById(id);
        return Result.build(ConstantEnum.SUCCESS).setResult(menu);
    }

    @ApiOperation("权限菜单修改方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menu",value = "menu实体类",required = true,paramType = "body")
    })
    @PutMapping
    public Result updateMenuInfo(@RequestBody Menu menu){
        boolean b = iMenuService.updateMenuInfo(menu);
        if (b){
            return Result.build(ConstantEnum.SUCCESS);
        }
        return Result.build(ConstantEnum.ERROR);
    }

    @ApiOperation(("权限菜单删除方法"))
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "menu实体类Id",required = true,paramType = "path")
    })
    @DeleteMapping("/{id}")
    public Result deleteMenuInfo(@PathVariable("id")Integer id){
        boolean b = iMenuService.deleteMenuInfo(id);
        if (b){
            return Result.build(ConstantEnum.SUCCESS);
        }
        return Result.build(ConstantEnum.ERROR);
    }

    @ApiOperation("角色权限调用修改查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rid",value = "实体类role主键Id",required = true,paramType = "path")
    })
    @GetMapping("r/{rid}")
    public Result getRoleMenu(@PathVariable("rid")Integer rid){
        List<Map<String,Object>> list = iMenuService.getRoleMenu(rid);
        return Result.build(ConstantEnum.SUCCESS).setResult(list);
    }
}
